#ifndef GLIMAGEWIDGET_H
#define GLIMAGEWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLShaderProgram>
#include <QElapsedTimer>
#include <QOpenGLTexture>
#include <QDebug>
#include "PluginInterface.h"

#include "qtimer.h"



class PLUGINCORE_EXPORT GLImageWidget : public QOpenGLWidget
{
    Q_OBJECT
    public:
    enum ImageShowPos {
        KeepPos, // 按上次位置显示
        KeepLeftTop,
        KeepLeft,
        KeepTop,
        Orignal,  // 显示原图
        FitWindow // 按窗口大小显示
      };

        Q_ENUM(ImageShowPos)

    public:
      GLImageWidget(QWidget *parent = 0);
      GLImageWidget(PluginInterface *p, QWidget *parent = nullptr);
      //    ~GLImageWin();

      PluginInterface *plugin=0;
      QTimer timer;

      QImage getImage();
      QImage getSeletedImage();
      QRect getSeleted();
      void processMsg(void *win);
      void setImage(QImage &img);
      void setImageShowPos(ImageShowPos aShowPos= FitWindow);
      void calculate();
      ImageShowPos showPos = FitWindow;
    public slots:
      void slot_setImage(QImage img);
  signals:
      void sig_repaint();
      void sig_setImage(QImage img);
    protected:
      void keyPressEvent(QKeyEvent *event)override;
      void paintEvent(QPaintEvent *event) override;
      //   void initializeGL();//初始化函数，在Widget刚加载时被调用
      //   void paintGL();//绘图函数，每一次绘图请求产生，都会进行一次绘图
      void resizeGL(int w, int h) override; // 用于处理窗口大小变化的情况
      // 鼠标事件
      void wheelEvent(QWheelEvent *e) override;
      void mouseMoveEvent(QMouseEvent *e) override;
      void mousePressEvent(QMouseEvent *e) override;
      void mouseReleaseEvent(QMouseEvent *e) override;
      void mouseDoubleClickEvent(QMouseEvent *e) override;

    private:
      QImage image;
        //以下参数用于鼠标右键，用于选择区域

      //鼠标在区域的哪个位置
      enum AreaPosition : int
      {
          Outside = 0x00,
          Inside = 0xFF, //任意值
          AtLeft = 0x01,
          AtRight = 0x02,
          AtTop = 0x10,
          AtBottom = 0x20,
          AtTopLeft = 0x11, //AtLeft|AtTop
          AtTopRight = 0x12, //AtRight|AtTop
          AtBottomLeft = 0x21, //AtLeft|AtBottom
          AtBottomRight = 0x22 //AtRight|AtBottom
      };
      //当前编辑类型
      enum EditType : int
      {
          EditNone, //无操作
          PressInside, //在选区范围内按下
          PressOutside, //在选区范围外按下
          DrawSelection, //绘制
          MoveSelection, //拖动
          EditSelection //拉伸编辑
      };

      //计算鼠标相对区域的位置
      AreaPosition calcPosition(const QPoint &pos);
      //当前鼠标对应选区的位置
      void setCurPosition(AreaPosition position);
      //根据鼠标当前位置更新鼠标样式
      void updateCursor();

      //当前选区
      //QRect有四个成员变量，分别对应左上角和右下角点坐标
      //x1-左上角坐标x
      //x2-等于x1+width-1
      //y1-左上角坐标y
      //y2-等于y1+height-1
      //即QRect(50,50,200,200)时，topLeft=(50,50)bottomRight=(249,249)
      //fillRect会填充整个区域
      //drawRect在画笔宽度奇数时，右下角会多1px，绘制时整体宽度先减去1px
      QRect selection;
      //是否有选区
      bool hasSelection{false};
      //鼠标当前操作位置
      AreaPosition curPosition{AreaPosition::Outside};
      //当前操作类型
      EditType curEditType{EditType::EditNone};
      //鼠标按下标志
      bool pressFlagRight{false};
      //鼠标按下位置
      QPoint pressPos;
      //目前用于记录press时鼠标与选区左上角的坐标差值
      QPoint tempPos;
      //鼠标当前位置
      QPoint mousePos;

      //最小宽度
      static const int Min_Width{5};
};

#endif // GLIMAGEWIDGET_H
